package com.airsystem.pos.bengkel.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hibernate.annotations.ForeignKey;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */

@Entity
@Table(name = "tbl_tagihan")
public class Tagihan implements Serializable {

	@Transient
	public static final String ALIAS = "tagihan";

	@Transient
	private Type choice;

	@Id
	private String periode;

	@Id
	@ManyToOne
	@JoinColumn(name = "jenis_tagihan", nullable = false)
	@ForeignKey(name = "FK_JENIS_TAGIHAN")
	private JenisTagihan jenis_tagihan;

	@Column(nullable = false)
	private Long biaya;

	public Tagihan() {
		choice = Type.PERIODE;
		periode = "";
	}

	public String getSearchColumn() {
		StringBuffer sb = new StringBuffer();

		switch (choice) {
		case PERIODE:
			return sb.append(ALIAS).append(".periode").toString();

		default:
			return sb.append(ALIAS).append(".periode").toString();
		}
	}

	public String getSearchKeyword() {
		switch (choice) {
		case PERIODE:
			return periode.toString();

		default:
			return "";
		}
	}

	public Type getChoice() {
		return choice;
	}

	public void setChoice(int choice) {
		switch (choice) {

		default:
			this.choice = Type.PERIODE;
			break;
		}
	}

	public enum Type {
		PERIODE(1);

		private int flag;

		Type(int number) {
			this.flag = number;
		}

		public int getFlag() {
			return flag;
		}
	}

	public String getPeriode() {
		return periode;
	}

	public void setPeriode(String periode) {
		this.periode = periode;
	}

	public JenisTagihan getJenis_tagihan() {
		return jenis_tagihan;
	}

	public void setJenis_tagihan(JenisTagihan jenis_tagihan) {
		this.jenis_tagihan = jenis_tagihan;
	}

	public Long getBiaya() {
		return biaya;
	}

	public void setBiaya(Long biaya) {
		this.biaya = biaya;
	}

	public boolean equals(Object object) {
		if (object instanceof Tagihan == false) {
			return false;
		}

		if (this == object) {
			return true;
		}

		Tagihan that = (Tagihan) object;

		return new EqualsBuilder().append(this.getJenis_tagihan(), that.getJenis_tagihan()).isEquals();
	}

	public int hashCode() {
		return new HashCodeBuilder().append(this.getJenis_tagihan()).hashCode();
	}
}